/**
 * PollingService.java
 * Copyright 2014-10-7 FengXinZi,all right reserved.
 * FengXinZi PROPRIETARY/CONFIDENTIAL, any from of usage is subject to approval.
 */
/**
 *
 */
package com.ikangtai.shecare.personal;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.main.SplashActivity;


public class PollingService extends Service {

    public static final String ACTION = "com.ikangtai.shecare.service.PollingService";
    public static final long DAY_MILLISECONDS = 24 * 60 * 60 * 1000;

    private Notification mNotification;
    private NotificationManager mManager;

    @Override
    public IBinder onBind(final Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        initNotificationManager();
    }

    @Override
    public void onStart(final Intent intent, final int startId) {

        if (null == intent) {
            return;
        }
//        if (intent.getIntExtra("notificationType", 0) == PollingUtil.NOTIFICATION_DAY) {
//            showNotification("您已三天没有测温了，快打开我，继续您的计划吧！");
//        } else {
        showNotification(getString(R.string.notification_title));
//        }
    }

    // 初始化通知栏配置
    private void initNotificationManager() {
        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final int icon = R.drawable.ic_launcher;
        mNotification = new Notification();
        mNotification.icon = icon;
        mNotification.tickerText = (getResources().getString(R.string.app_notification));
//        mNotification.defaults |= Notification.DEFAULT_ALL;
        mNotification.flags = Notification.FLAG_AUTO_CANCEL;
    }

    // 弹出Notification
    private void showNotification(final String content) {
        mNotification.when = System.currentTimeMillis();
        // Navigator to the new activity when click the notification title
        final Intent intent = new Intent();
        intent.setClass(this, SplashActivity.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        mNotification = new Notification.Builder(this)
                .setAutoCancel(true)
                .setSound(Uri.parse("file:///android_asset/spring_memories_alarm.m4a"))
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(getResources().getString(R.string.app_notification))
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.share_launch_icon)
                .setWhen(System.currentTimeMillis())
                .build();

        mManager.notify(0, mNotification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Service:onDestroy");
    }

}
